define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    $('#show_putong').hide();
    $('#show_yimiao').hide();
    $('#show_jiuzhen').hide();
    $('#c-typeid').change(function(){
        // 获取选中的值
        var value = $(this).val();
        // 根据选中值显示对应字段
        if(value == 'yimiao'){
            $('#show_putong').hide();
            $('#show_jiuzhen').hide();
            $('#show_yimiao').show();
        }else if(value == 'putong'){
            $('#show_yimiao').hide();
            $('#show_jiuzhen').hide();
            $('#show_putong').show();
        }else{
            $('#show_yimiao').hide();
            $('#show_putong').hide();
            $('#show_jiuzhen').show();
        }
    });
    $('#c-typeid').trigger('change');
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'message/index' + location.search,
                    add_url: 'message/add',
                    edit_url: 'message/edit',
                    sendmb_url: 'message/sendmb',
                    del_url: 'message/del',
                    readall_url: 'message/readall',
                    multi_url: 'message/multi',
                    table: 'message'
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'message_id',
                sortName: 'message_id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'message_id', title: __('Message_id')},
                        {field: 'message_type', title: __('Message_type'), searchList: {"system":__('System'),"user":__('User')}, formatter: Table.api.formatter.normal},
                        {field: 'message_title', title: __('Message_title')},
                        {field: 'message_to', title: '接收者'},
                        {field: 'status', title: '切换状态', table: table, formatter: Table.api.formatter.toggle,searchable:false},
                        {field: 'createtime', title: __('Createtime'),sortable: true, operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'),sortable: true, operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

        },
        add: function () {
            Controller.api.showUser();
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.showUser();
            Controller.api.bindevent();
        },
        sendmb: function () {

            if (typeof Layer !== 'undefined') {
                Layer.closeAll();
            }
            Controller.api.showUser();
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            showUser:function(){
                $('body').on('change', '[data-show-user]', function () {
                    if($(this).val() == 'user'){
                        $("#selectpage_user").append(`
<input id="c-user_id" 
placeholder="请选择会员" 
data-rule="required" 
data-source="vppz/user/messageindex" 
data-field="nickname" 
class="form-control selectpage" 
name="row[user_id]" 
type="text" value="">`);
                        Form.events.selectpage($("#selectpage_user"));
                    }else{
                        $("#selectpage_user").html('');
                    }
                });
            }
        }
    };
    return Controller;
});
